package com.buba.springcloud.controller;

import com.buba.springcloud.entities.CommonResult;
import com.buba.springcloud.entities.HfCoupon;
import com.buba.springcloud.entities.HfCouponReceiveRecord;
import com.buba.springcloud.entities.HfCouponUseRecord;
import com.buba.springcloud.service.CouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author taodeyu
 * @Date 2022/1/6 下午 2:31
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/backstagesupporter/coupon")
public class CouponController {
    @Resource
    private CouponService couponService;

    /**
     * 全查优惠券
     * @return
     */
    @GetMapping("/getAllCoupon")
    public CommonResult getAllCoupon(){
       List<HfCoupon> hfCoupons = couponService.getAllCoupon();
       if (hfCoupons != null){
           return new CommonResult(200,"查询成功",hfCoupons);
       }else{
           return new CommonResult(500,"抱歉查询失败");
       }
    }

    /**
     * 优惠券领取记录全查
     * @return
     */
    @GetMapping("/getAllReceiveRecordByCoupon")
    public CommonResult getAllReceiveRecordByCoupon(Integer id){
        List<HfCouponReceiveRecord> hfCouponReceiveRecords = couponService.getAllReceiveRecordByCoupon(id);
        if (hfCouponReceiveRecords != null){
            return new CommonResult(200,"查询成功",hfCouponReceiveRecords);
        }else{
            return new CommonResult(500,"抱歉查询失败");
        }
    }
    /**
     * 优惠券使用记录全查
     * @return
     */
    @GetMapping("/getAllUseRecordByCoupon")
    public CommonResult getAllUseRecordByCoupon(Integer id){
        List<HfCouponUseRecord> hfCouponUseRecords = couponService.getAllUseRecordByCoupon(id);
        if (hfCouponUseRecords != null){
            return new CommonResult(200,"查询成功",hfCouponUseRecords);
        }else{
            return new CommonResult(500,"抱歉查询失败");
        }
    }

    /**
     * 保存优惠券方法
     * @return
     */
    @PostMapping("/saveCoupon")
    public CommonResult saveCoupon(@RequestBody HfCoupon hfCoupon){
        Integer i =  couponService.saveCoupon(hfCoupon);
        if (i ==1){
            return new CommonResult(200,"添加成功",couponService.getAllCoupon());
        }
        return new CommonResult(500,"添加失败");
    }

    /**
     *优惠券开关
     */
    @PostMapping("/alterSwitch")
    public CommonResult alterSwitch(@RequestBody HfCoupon hfCoupon){
        Integer i = couponService.alterSwitch(hfCoupon);
        if (i ==1){
            return new CommonResult(200,"更改成功",null);
        }
        return new CommonResult(500,"更改失败");
    }

    /**
     * 修改优惠券方法
     * @param hfCoupon
     * @return
     */
    @PostMapping("/updateCoupon")
    public CommonResult updateCoupon(@RequestBody HfCoupon hfCoupon){
        Integer i = couponService.updateCoupon(hfCoupon);
        if (i ==1){
            return new CommonResult(200,"添加成功",couponService.getAllCoupon());
        }
        return new CommonResult(500,"添加失败");
    }
}
